package pers.ht.mbpo.dao;

import pers.ht.mbpo.bean.Match;
import pers.ht.mbpo.util.DBUtil;
import pers.ht.mbpo.util.DaoUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MatchDao {
    //添加
    public void insert(Match m) {
        String sql = "INSERT INTO matchs(matchId,matchName,beginTime,endTime,maxNumber,isLock) VALUES (?,?,?,?,?,?)";
        DaoUtil.update(sql, m.getMatchId(), m.getMatchName(), m.getBeginTime(), m.getEndTime(), m.getMaxNumber(), "0", "<a title=\"报名\" class=\"layui-btn layui-btn layui-btn-xs\" lay-event=\"sign\">报名</a>");
    }

    //删除
    public void delete(String id) {
        String sql = "DELETE FROM matchs WHERE matchId = ?";
        DaoUtil.update(sql, id);
    }

    //修改
    public void edit(Match m) {
        String sql = "UPDATE matchs SET matchName = ?,beginTime = ?,endTime = ?,maxNumber = ? WHERE matchId = ?";
        DaoUtil.update(sql, m.getMatchName(), m.getBeginTime(), m.getEndTime(), m.getMaxNumber(), m.getMatchId());
    }

    //锁定
    public void lock(String id) {
        String sql = "UPDATE matchs SET isLock = 1  WHERE matchId = ?";
        DaoUtil.update(sql, id);
    }

    //解锁
    public void unLock(String id) {
        String sql = "UPDATE matchs SET isLock = 0  WHERE matchId = ?";
        DaoUtil.update(sql, id);
    }

    //校验主键唯一
    public boolean checkId(String id) {
        return DaoUtil.checkId("match", "matchId", id);
    }

    //查询所有
    public List<Match> query() {
        CompetitorDao competitorDao = new CompetitorDao();
        List<Match> matchs = new ArrayList<Match>();
        String sql = "SELECT * from matchs";
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            stat = conn.createStatement();
            rs = stat.executeQuery(sql);
            while (rs.next()) {
                Match m = new Match();
                m.setMatchId(rs.getString("matchId"));
                int num = competitorDao.queryNum(m.getMatchId(), null);
                m.setMatchName(rs.getString("matchName"));
                m.setBeginTime(rs.getDate("beginTime").toString());
                m.setEndTime(rs.getDate("endTime").toString());
                m.setMaxNumber(rs.getInt("maxNumber"));
                m.setNum(num);
                m.setIsLock(rs.getString("isLock"));
                m.setMatchId(rs.getString("matchId"));
                m.setRemark(rs.getString("remark"));
                matchs.add(m);
            }
            DBUtil.close(rs, stat, conn);
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        return matchs;
    }

    //按主键查询一个
    public Match queryOne(String matchId) {
        Match m = new Match();
        String sql = "SELECT * from matchs WHERE matchId = '" + matchId + "'";
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            stat = conn.createStatement();
            rs = stat.executeQuery(sql);
            while (rs.next()) {
                m.setMatchId(rs.getString("matchId"));
                m.setMatchName(rs.getString("matchName"));
                m.setBeginTime(rs.getDate("beginTime").toString());
                m.setEndTime(rs.getDate("endTime").toString());
                m.setMaxNumber(rs.getInt("maxNumber"));
                m.setIsLock(rs.getString("isLock"));
                m.setMatchId(rs.getString("matchId"));
            }
            DBUtil.close(rs, stat, conn);
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        return m;
    }

    //报名是否开始
    public boolean ifBegin(String begin) {
        boolean flag = true;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();
        String str = sdf.format(now);
        try {
            Date beginTime = sdf.parse(begin);
            Date nowTime = sdf.parse(str);
            if (nowTime.before(beginTime)) {
                flag = false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return flag;
    }
    //报名是否结束
    public boolean ifEnd(String end) {
        boolean flag = false;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();
        String str = sdf.format(now);
        try {
            Date beginTime = sdf.parse(end);
            Date nowTime = sdf.parse(str);
            if (nowTime.after(beginTime)) {
                flag = true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return flag;
    }



}
